<?php

namespace App\Models\Purchase;

use App\Models\Manger\Manger;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class PurchaseStockinOrder extends Model
{
    protected $table = 'purchase_stockin_order';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];


    /**
     *获取列表 
     */
    public static function getList(Request $request, $cpid, $manger_id = 0, $isgetall = 0)
    {
        $query = self::with('suborder')->where(['cpid' => $cpid, 'is_del' => 1])->orderBy('create_at', 'desc');
        if ($manger_id > 0) {
            $query->where('manger_id', $manger_id);
        }




        $pa_type = $request->pa_type;
        if (in_array($pa_type, [1, 2, 3,4])) {
            $query->where('pa_type', $pa_type);
        }

        $pa_id = intval($request->pa_id);
        if ($pa_id>0) {
            $query->where('pa_id', $pa_id);
        }


        $order_id = intval($request->order_id);//根据采购订单查询进货单
        if ($order_id>0) {
            $query->where('order_id', $order_id);
        }

        $sales_manger_name=$request->sales_manger_name;
        if($sales_manger_name!=''){
            $query->where('sales_manger_name','like','%'.$sales_manger_name.'%');
        }
        
        $pa_settle_company_name = $request->pa_settle_company_name; //结算公司名称
        if ($pa_settle_company_name != '') {
            $query->where('pa_settle_company_name', 'like', '%' . $pa_settle_company_name . '%');
        }
        $company_name = $request->company_name; //供应商名称
        if ($company_name != '') {
            $query->where('company_name', 'like', '%' . $company_name . '%');
        }


        $po_order_sn_code = $request->po_order_sn_code; //采购订单单据编号
        if ($po_order_sn_code != '') {
            $query->where('po_order_sn_code', 'like', '%' . $po_order_sn_code . '%');
        }


        $warehouse_name = $request->warehouse_name;
        if ($warehouse_name != '') {
            $order_arr = PurchaseStockinOrderSub::where('warehouse_name', 'like', '%' . $warehouse_name . '%')->where(['cpid' => $cpid])->pluck('stockin_order_id');
            $query->whereIn('id', $order_arr);
        }

        $archive_no = $request->archive_no;
        if ($archive_no != '') {
            $order_arr = PurchaseStockinOrderSub::where('archive_no', 'like', '%' . $archive_no . '%')->where(['cpid' => $cpid])->pluck('stockin_order_id');
            $query->whereIn('id', $order_arr);
        }

        $archive_spec = $request->archive_spec;
        if ($archive_spec != '') {
            $order_arr = PurchaseStockinOrderSub::where('archive_spec', 'like', '%' . $archive_spec . '%')->where(['cpid' => $cpid])->pluck('stockin_order_id');
            $query->whereIn('id', $order_arr);
        }

        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }


    /**
     * 获取子订单
     */
    public function suborder()
    {
        return $this->hasMany(PurchaseStockinOrderSub::class, 'stockin_order_id', 'id')->where('is_del',1);
    }

    public function createmangerinfo(){
        return $this->hasOne(Manger::class,'id','create_manger_id');
    }
    
    public function mangerinfo(){
        return $this->hasOne(Manger::class,'id','manger_id');
    }
}
